package cn.fintecher.gis.web;


import cn.fintecher.gis.annotation.CurrentOperator;
import cn.fintecher.gis.entity.PatrolInfo;
import cn.fintecher.gis.entity.PatrolTrack;
import cn.fintecher.gis.enums.PatrolType;
import cn.fintecher.gis.model.request.PatrolInfoRequest;
import cn.fintecher.gis.model.request.PatrolResultRequest;
import cn.fintecher.gis.model.request.QueryPatrolInfoRequest;
import cn.fintecher.gis.model.request.TrackRequest;
import cn.fintecher.gis.model.response.PatrolAppResponse;
import cn.fintecher.gis.model.response.PatrolBusinessResponse;
import cn.fintecher.gis.model.response.PatrolFileResponse;
import cn.fintecher.gis.model.response.PatrolInfoResponse;
import cn.fintecher.gis.model.session.SessionModel;
import cn.fintecher.gis.service.PatrolInfoService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/api/patrolInfoController")
@Api(value = "巡查任务管理", description = "巡查任务管理")
public class PatrolInfoController {

    private final Logger logger = LoggerFactory.getLogger(PatrolInfoController.class);

    @Autowired
    private PatrolInfoService patrolInfoService;

    @GetMapping("/queryPatrolInfoAll")
    @ApiOperation(value = "查询巡查信息", notes = "查询巡查信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<PatrolInfoResponse>> queryPatrolInfoAll(QueryPatrolInfoRequest request,
                                                                       @ApiIgnore @CurrentOperator SessionModel sessionModel,
                                                                       Pageable pageable) {
        Page<PatrolInfoResponse> page = patrolInfoService.queryPatrolInfoAll(request, sessionModel, pageable);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/queryPatrolInfo")
    @ApiOperation(value = "查询巡查信息", notes = "查询巡查信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<PatrolInfoResponse>> queryPatrolInfo(QueryPatrolInfoRequest request,
                                                                    @ApiIgnore @CurrentOperator SessionModel sessionModel,
                                                                    Pageable pageable) {
        Page<PatrolInfoResponse> page = patrolInfoService.queryPatrolInfoList(request, pageable);
        return ResponseEntity.ok().body(page);
    }


    @GetMapping("/queryPatrolInfoComplete")
    @ApiOperation(value = "查询巡查信息(已巡查)", notes = "查询巡查信息(已巡查)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<PatrolInfoResponse>> queryPatrolInfoComplete(@RequestParam(value = "type", required = false) PatrolType type,
                                                                            Pageable pageable) {
        Page<PatrolInfoResponse> page = patrolInfoService.queryPatrolInfo(type, pageable);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/getPatrolInfos")
    @ApiOperation(value = "查询巡查信息(已巡查并归档)", notes = "查询巡查信息(已巡查并归档)")
    public ResponseEntity<List<PatrolInfoResponse>> getPatrolInfos() {
        List<PatrolInfoResponse> list = patrolInfoService.getPatrolInfos();
        return ResponseEntity.ok().body(list);
    }

    @GetMapping("/queryPatrolInfoByApp")
    @ApiOperation(value = "查询巡查信息", notes = "查询巡查信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<PatrolAppResponse>> queryPatrolInfoByApp(QueryPatrolInfoRequest request,
                                                                        @ApiIgnore @CurrentOperator SessionModel sessionModel,
                                                                        Pageable pageable) {
        Page<PatrolAppResponse> page = patrolInfoService.queryPatrolInfoApp(request, sessionModel, pageable);
        return ResponseEntity.ok().body(page);
    }


    @GetMapping("/getPatrolByIdApp")
    @ApiOperation(value = "根据id查询巡查信息", notes = "根据id查询巡查信息")
    public ResponseEntity<PatrolAppResponse> getPatrolByIdApp(@RequestParam("id") String id) {
        PatrolAppResponse response = patrolInfoService.getByIdApp(id);
        return ResponseEntity.ok().body(response);
    }

    @GetMapping("/getPatrolInfoById")
    @ApiOperation(value = "根据id查询对应的巡查信息", notes = "根据id查询对应的巡查信息")
    public ResponseEntity<PatrolFileResponse> getPatrolInfoById(@RequestParam("id") String id) {
        PatrolFileResponse response = patrolInfoService.getPatrolInfoById(id);
        return ResponseEntity.ok().body(response);
    }

    @GetMapping("/getPatrolTrack")
    @ApiOperation(value = "查询巡查结果", notes = "查询巡查结果")
    public ResponseEntity<List<PatrolTrack>> getPatrolTrack(@RequestParam @ApiParam(value = "id", required = true) String id) {
        List<PatrolTrack> list = patrolInfoService.getPatrolTrack(id);
        return ResponseEntity.ok().body(list);
    }

    @GetMapping("/getPatrolBusinessResponse")
    @ApiOperation(value = "根据id查询基本信息", notes = "根据id查询基本信息")
    public ResponseEntity<PatrolBusinessResponse> getPatrolBusinessResponse(@RequestParam @ApiParam(value = "id", required = true) String id) {
        PatrolBusinessResponse patrolBusinessResponse = patrolInfoService.getPatrolBusinessResponse(id);
        return ResponseEntity.ok().body(patrolBusinessResponse);
    }


    @DeleteMapping("/deletePatrolInfoById/{id}")
    @ApiOperation(value = "巡查信息删除", notes = "巡查信息删除")
    public ResponseEntity<Void> deletePatrolInfoById(@PathVariable String id) {
        patrolInfoService.deletePatrolInfoById(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/createPatrolInfo")
    @ApiOperation(value = "巡查信息维护", notes = "巡查信息维护")
    public ResponseEntity<Void> createPatrolInfo(@RequestBody PatrolInfoRequest request,
                                                 @ApiIgnore @CurrentOperator SessionModel sessionModel) throws Exception {
        patrolInfoService.createPatrolInfo(request, sessionModel);
        return ResponseEntity.ok().build();
    }



    @PostMapping("/addPatrolResult")
    @ApiOperation(value = "添加巡查结果", notes = "添加巡查结果")
    public ResponseEntity<Void> addPatrolResult(@RequestBody PatrolResultRequest request) {
        patrolInfoService.addPatrolResult(request);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/addPatrolFile")
    @ApiOperation(value = "添加巡查文件", notes = "添加巡查文件")
    public ResponseEntity<Void> addPatrolFile(@RequestBody PatrolResultRequest request) {
        patrolInfoService.addPatrolFile(request);
        return ResponseEntity.ok().build();
    }


    @PostMapping("/addTrackInfo")
    @ApiOperation(value = "添加巡查轨迹", notes = "添加巡查轨迹")
    public ResponseEntity<Void> addTrackInfo(@RequestBody TrackRequest request) {
        patrolInfoService.addTrackInfo(request);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/signPatrolById")
    @ApiOperation(value = "巡查签到", notes = "巡查签到")
    public ResponseEntity<PatrolInfo> signPatrolById(@RequestBody PatrolResultRequest request) {
        PatrolInfo patrolInfo = patrolInfoService.signPatrolById(request);
        return ResponseEntity.ok().body(patrolInfo);
    }


//    @GetMapping("/queryCompleteList")
//    @ApiOperation(value = "拉取已完成外业任务列表", notes = "拉取已完成外业任务列表")
//    public ResponseEntity<Iterable<PatrolInfoResponse>> queryPatrolInfoAll() {
//        Iterable<PatrolInfoResponse> result = patrolInfoService.queryCompleteList();
//        return ResponseEntity.ok().body(result);
//    }

}
